import process from 'child_process'
import chalk from 'chalk'
import ora from 'ora'
import log from '../utils/log'

const spinner = ora({
  text: 'cli updating...',
  spinner: {
    interval: 300,
    frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
  }
})

export function update() {
  spinner.start()
  process.exec('npm install rysx-cli@latest -g', err => {
    spinner.stop()
    if (!err) {
      log.success(chalk.green('更新成功'))
    } else {
      log.error(chalk.red('更新失败'))
    }
  })
}